﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace WeaponEvalue.Logic
{
    public enum LogType
    {
        SHIJIU,
        SHANDIAN
    }

    public class ThreeDimension
    {
        public int GameIndex;
        public int x;
        public int y;
        public int z;
    }

    public class LogHelper
    {
        private readonly List<ThreeDimension> shijiu = new List<ThreeDimension>();
        private readonly List<ThreeDimension> shandian = new List<ThreeDimension>();
        private static LogHelper loghelpter;

        #region Delegates

        public delegate void AppendControlTextDelegate(Control control, string text);

        #endregion

        private static void AppendControlText(Control control, string text)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new AppendControlTextDelegate(AppendControlText), new object[] { control, text });
            }
            else
            {
                control.Text += text;
            }
        }

        public static LogHelper GetLogger()
        {
            if (loghelpter==null)
            {
                loghelpter=new LogHelper();
                loghelpter.LoadLog();
            }
            return loghelpter;
        }

        public void LoadLog()
        {
            if (File.Exists("Log\\狮鹫.dat"))
            {
                var temp = File.ReadAllBytes("Log\\狮鹫.dat");
                for (var i = 0; i < temp.Length; )
                {
                    var threeDimension = new ThreeDimension
                                             {
                                                 x = BitConverter.ToInt32(temp, i),
                                                 y = BitConverter.ToInt32(temp, i + 4),
                                                 z = BitConverter.ToInt32(temp, i + 8)
                                             };
                    shijiu.Add(threeDimension);
                    i += 12;
                }
            }
            if (File.Exists("Log\\闪电.dat"))
            {
                var temp = File.ReadAllBytes("Log\\闪电.dat");
                for (var i = 0; i < temp.Length; )
                {
                    var threeDimension = new ThreeDimension
                                             {
                                                 x = BitConverter.ToInt32(temp, i),
                                                 y = BitConverter.ToInt32(temp, i + 4),
                                                 z = BitConverter.ToInt32(temp, i + 8)
                                             };
                    shandian.Add(threeDimension);
                    i += 12;
                }
            }
        }

        public List<ThreeDimension> GetLog(LogType type)
        {
            var threeDimensions=new List<ThreeDimension>();

            if (type == LogType.SHIJIU)
            {
                lock (shijiu)
                {
                    threeDimensions.AddRange(shijiu);
                }
            }
            if (type == LogType.SHANDIAN)
            {
                lock (shandian)
                {
                    threeDimensions.AddRange(shandian);
                }
            }
            return threeDimensions;
        }

        public void Flush()
        {
            lock (shijiu)
            {
                if(shijiu.Count>0)
                {
                    var fs = new FileStream("Log\\狮鹫.dat",FileMode.Create);
                    foreach (var dimension in shijiu)
                    {
                        fs.Write(BitConverter.GetBytes(dimension.x), 0, 4);
                        fs.Write(BitConverter.GetBytes(dimension.y), 0, 4);
                        fs.Write(BitConverter.GetBytes(dimension.z), 0, 4);
                    }
                    fs.Flush();
                    fs.Close();
                }
            }
            lock (shandian)
            {
                if(shandian.Count>0)
                {
                    var fs = new FileStream("Log\\闪电.dat", FileMode.Create);
                    foreach (var dimension in shandian)
                    {
                        fs.Write(BitConverter.GetBytes(dimension.x), 0, 4);
                        fs.Write(BitConverter.GetBytes(dimension.y), 0, 4);
                        fs.Write(BitConverter.GetBytes(dimension.z), 0, 4);
                    }
                    fs.Flush();
                    fs.Close();
                }
            }
        }

        public void AppendLog(GameMonitor dimension, int x, int y, int z)
        {
            AppendControlText(dimension.LogTextBox, string.Format("发现{0} 位置{1},{2},{3} {4}\r\n", dimension.NpcName,x,y,z,DateTime.Now));
            if(dimension.NpcName.StartsWith("雷"))
            {
                shandian.Add(new ThreeDimension(){GameIndex = dimension.ID,x=x,y=y,z=z});
            }
            else if (dimension.NpcName.StartsWith("狮"))
            {
                shijiu.Add(new ThreeDimension() { GameIndex = dimension.ID, x = x, y = y, z = z });
            }
        }
    }
}